package com.ifast.yuxin.controller;


import java.util.Arrays;

import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ifast.common.annotation.Log;
import com.ifast.common.base.AdminBaseController;
import com.ifast.yuxin.domain.QuestionsDO;
import com.ifast.yuxin.service.QuestionsService;
import com.ifast.common.utils.Result;

/**
 * 
 * <pre>
 * 娱信竞技场数据基础表
 * </pre>
 * <small> 2019-02-06 21:14:49 | zhengyingjun</small>
 */
@Controller
@RequestMapping("/yuxin/questions")
public class QuestionsController extends AdminBaseController {
	@Autowired
	private QuestionsService questionsService;
	
	@GetMapping()
	@RequiresPermissions("yuxin:questions:questions")
	String Questions(){
	    return "yuxin/questions/questions";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("yuxin:questions:questions")
	public Result<Page<QuestionsDO>> list(QuestionsDO questionsDTO){
        Wrapper<QuestionsDO> wrapper = new EntityWrapper<QuestionsDO>().orderBy("id", false);
        Page<QuestionsDO> page = questionsService.selectPage(getPage(QuestionsDO.class), wrapper);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("yuxin:questions:add")
	String add(){
	    return "yuxin/questions/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("yuxin:questions:edit")
	String edit(@PathVariable("id") Integer id,Model model){
		QuestionsDO questions = questionsService.selectById(id);
		model.addAttribute("questions", questions);
	    return "yuxin/questions/edit";
	}
	
	@Log("添加娱信竞技场数据基础表")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("yuxin:questions:add")
	public Result<String> save( QuestionsDO questions){
		questionsService.insert(questions);
        return Result.ok();
	}
	
	@Log("修改娱信竞技场数据基础表")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("yuxin:questions:edit")
	public Result<String>  update( QuestionsDO questions){
		boolean update = questionsService.updateById(questions);
		return update ? Result.ok() : Result.fail();
	}
	
	@Log("删除娱信竞技场数据基础表")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("yuxin:questions:remove")
	public Result<String>  remove( Integer id){
		questionsService.deleteById(id);
        return Result.ok();
	}
	
	@Log("批量删除娱信竞技场数据基础表")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("yuxin:questions:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] ids){
		questionsService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	@Log("批量想借口导入数据")
	@PostMapping( "/importData")
	@ResponseBody
	@RequiresPermissions("yuxin:questions:importData")
	public String  importData(@RequestParam("ids[]") Integer[] ids){
		/**
		 * 调用service接口导入数据
		 * 参数：根据平台数据结构将id数组转化为集合
		 * 返回值：传入数量等信息
		 */
		JSONObject jo = new JSONObject();
		String message = questionsService.importInterface(Arrays.asList(ids));
		//todo 连接超时问题解决
		jo.put("message",message);
		return jo.toString();
	}
}
